package com.mvp.shq.washer.ui.shop.shopCollection;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.OrderAdapter;
import com.mvp.shq.washer.adapter.ShopCollectionAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.entity.shop.result.CollectResult;
import com.mvp.shq.washer.ui.shop.productDetail.ProductDetailActivity;
import com.mvp.shq.washer.utils.GridDivider;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.OnItemClickListener;
import com.mvp.shq.washer.utils.RecyclerSpace;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopCollectionActivity extends BaseActivity<Presenter, Model> implements ShopCollectionConstract.View {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private List<Product> products;
    private ShopCollectionAdapter adapter;
    private int pageNo = 1;
    private int pageSize = 10;
    private boolean isLoadMore = false;
    private boolean hasMoreDate = true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_shop_collection;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        title.setText("我的收藏");
        recyclerview.setLayoutManager(new GridLayoutManager(context,2));
        RecyclerSpace divider = new RecyclerSpace(5,ContextCompat.getColor(context,R.color.shop_divison_bg));
        recyclerview.addItemDecoration(divider);
        products = new ArrayList<>();
        adapter = new ShopCollectionAdapter(context,products);
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void onClick(int position) {
                JumpUtil.toProductDetail(context,products.get(position).getProductId(), ProductDetailActivity.PRODUCT_TYPE_SPU);
            }
        });
        recyclerview.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                hasMoreDate = true;
                isLoadMore = false;
                pageNo = 1;
                presenter.getCollectList(pageNo,pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (hasMoreDate){
                    isLoadMore = true;
                    pageNo ++;
                    presenter.getCollectList(pageNo,pageSize);
                }else {
                    refreshlayout.finishLoadmore();
                }
            }
        });
        refreshLayout.autoRefresh();
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void getCollectListS(CollectResult result) {

        if (result.getList() == null){
            refreshLayout.finishLoadmore();
            refreshLayout.finishRefresh();
            return;
        }
         if (!isLoadMore){
             products.clear();
         }
        //当前数据最后一条的下标
        int currentNum = (pageNo - 1)*pageSize + result.getList().size();
        if (result.getCount() <= currentNum){
            hasMoreDate = false;
        }
         products.addAll(result.getList());
        adapter.notifyDataSetChanged();
        refreshLayout.finishLoadmore();
        refreshLayout.finishRefresh();
    }
}
